package lcy.homework01;

import java.util.Objects;

/**
 * @ClassName: Triangle
 * @Description:
 * @author: 李春洋
 * @date: 2024/3/30 16:26
 * @Blog: https://
 */
public class Triangle  extends Shape {
    Point[] vertices;

    Triangle(Point[] vertices) {
        this.vertices = vertices;
    }

    @Override
    double calculatePerimeter() {
        double perimeter = 0;
        perimeter += distance(vertices[0], vertices[1]);
        perimeter += distance(vertices[1], vertices[2]);
        perimeter += distance(vertices[2], vertices[0]);
        return perimeter;
    }

    @Override
    double calculateArea() {
        double area = 0.5 * Math.abs(
                (vertices[0].x - vertices[2].x) * (vertices[1].y - vertices[2].y) -
                        (vertices[1].x - vertices[2].x) * (vertices[0].y - vertices[2].y));
        return area;
    }

    @Override
    void move(Point p) {
        for (Point vertex : vertices) {
            vertex.x += p.x;
            vertex.y += p.y;
        }
    }

    private double distance(Point p1, Point p2) {
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Triangle)) return false;
        Triangle other = (Triangle) obj;
        return vertices[0].equals(other.vertices[0]) && vertices[1].equals(other.vertices[1]) && vertices[2].equals(other.vertices[2]);
    }

    @Override
    public int hashCode() {
        return Objects.hash(vertices[0], vertices[1], vertices[2]);
    }

    @Override
    public String toString() {
        return "三角形 [顶点=" + vertices[0] + ", " + vertices[1] + ", " + vertices[2] + "]";
    }
}
